﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using JCTimes.Portal.Infrastructure;
using System.Xml;
using JCTimes.DomainModel.Views;
using JCTimes.DomainModel.Repository;
using JCTimes.DomainModel.Entity;
using System.Web.Mvc;
using System.Text;

namespace JCTimes.Portal.WebService
{
    /// <summary>
    /// Summary description for JCTimesPassport
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class JCTimesPassport : System.Web.Services.WebService
    {
        private IPDARepository pRep = (IPDARepository)DependencyResolver.Current.GetService(typeof(IPDARepository));
        private IPDAUsersFunctionsRepository puserFunRep = (IPDAUsersFunctionsRepository)DependencyResolver.Current.GetService(typeof(IPDAUsersFunctionsRepository));
        private IExpressClassRepository exRep = (IExpressClassRepository)DependencyResolver.Current.GetService(typeof(IExpressClassRepository));
        private IPDAConnectLogRepository plRep = (IPDAConnectLogRepository)DependencyResolver.Current.GetService(typeof(IPDAConnectLogRepository));
        private IPDAVersionsRepository pvRep = (IPDAVersionsRepository)DependencyResolver.Current.GetService(typeof(IPDAVersionsRepository));

        /// <summary>
        /// 获取PDA版本
        /// </summary>
        /// <param name="pdano">PDA编号</param>
        /// <returns></returns>
        [WebMethod]
        public XmlDocument GetPDAVersions(string pdano)
        {
            string returnvalue = "";
            //准备返回结果
            XmlDocument xd = new XmlDocument();
            var el = xd.CreateElement("JCTimes");
            xd.AppendChild(el);

            // WebService 
            XmlElement elementWebService = xd.CreateElement("WebService");
            el.AppendChild(elementWebService);

            // ReturnValue
            XmlElement elementReturnValue = xd.CreateElement("ReturnValue");

            //判断PDA编号是否存在
            var pdainfo = pRep.Find(m => m.PDANo == pdano && m.Status==1);
            if (pdainfo != null)
            {
                //插入PDA访问日志
                PDAConnectLog pclinfo = new PDAConnectLog();
                pclinfo.ID = Guid.NewGuid().ToString();
                pclinfo.PDAID = pdainfo.ID;
                pclinfo.LogContent = pdano;
                pclinfo.PortName = "获取PDA版本";
                pclinfo.ConnectFlag = "合法";
                pclinfo.CreateDate = System.DateTime.Now;
                plRep.Add(pclinfo);
                plRep.SaveChanges();

                //查找对应下载版本
                var pdaversionList = pvRep.GetList<PDAVersion>().OrderByDescending(m => m.CreateDate).ToList();
                if (pdaversionList != null && pdaversionList.Count > 0)
                {
                    var pdaversioninfo = pdaversionList[0];
                    returnvalue = pdaversioninfo.PDAVersion1;
                }
                else
                {
                    returnvalue = "版本及下载地址为空";
                }
            }
            else
            {
                returnvalue = "-1";
                //插入PDA访问日志
                PDAConnectLog pclinfo = new PDAConnectLog();
                pclinfo.ID = Guid.NewGuid().ToString();
                pclinfo.LogContent = pdano;
                pclinfo.PortName = "获取PDA版本";
                pclinfo.ConnectFlag = "非法";
                pclinfo.CreateDate = System.DateTime.Now;
                plRep.Add(pclinfo);
                plRep.SaveChanges();
            }
            byte[] Array = Encoding.UTF8.GetBytes(returnvalue);
            string xml = Encoding.UTF8.GetString(Array);

            elementReturnValue.InnerText = xml;
            elementWebService.AppendChild(elementReturnValue);

            return xd;

        }

        /// <summary>
        /// 获取PDA功能和快递类别
        /// </summary>
        /// <param name="pdano">PDA编号</param>
        /// <returns></returns>
        [WebMethod]
        public XmlDocument GetPDAFunctionAndExpressClass(string pdano)
        {
            string returnvalue = "";
            string expressclass = "";
            string pdafunction = "";

            //准备返回结果
            XmlDocument xd = new XmlDocument();
            var el = xd.CreateElement("JCTimes");
            xd.AppendChild(el);

            // WebService 
            XmlElement elementWebService = xd.CreateElement("WebService");
            el.AppendChild(elementWebService);

            // ReturnValue
            XmlElement elementReturnValue = xd.CreateElement("ReturnValue");
            //ExpressClass
            XmlElement elementExpressClass = xd.CreateElement("ExpressClass");
            //PDAFunction
            XmlElement elementPDAFunction = xd.CreateElement("PDAFunction");

            //判断PDA编号是否存在
            var pdainfo = pRep.Find(m => m.PDANo == pdano && m.Status == 1);
            if (pdainfo != null)
            {
                //插入PDA访问日志
                PDAConnectLog pclinfo = new PDAConnectLog();
                pclinfo.ID = Guid.NewGuid().ToString();
                pclinfo.PDAID = pdainfo.ID;
                pclinfo.LogContent = pdano;
                pclinfo.ConnectFlag = "合法";
                pclinfo.PortName = "获取PDA功能和快递类别";
                pclinfo.CreateDate = System.DateTime.Now;
                plRep.Add(pclinfo);
                plRep.SaveChanges();

                returnvalue = "1";
                //查找对应的快递类别
                string usersid = pdainfo.UsersID;//对应的客户ID
                List<ExpressClass> eclist = exRep.GetList<ExpressClass>().Where(m => m.UsersID == usersid).ToList();
                if (eclist != null && eclist.Count > 0)
                {
                    foreach (var ecitem in eclist)
                    {
                        expressclass += ecitem.ExpressClassName;
                        expressclass += ";";
                    }
                }


                //查找该PDA对应的功能 如没有加载客户对应的功能
                List<V_PDAPDAFunctions> pdafunctionlist = puserFunRep.GetList<V_PDAPDAFunctions>().Where(m => m.PDAID == pdainfo.ID).OrderBy(m=>m.PDAFunctionNo).ToList();
                if (pdafunctionlist != null && pdafunctionlist.Count > 0)
                {
                    foreach (var pdafunctionitem in pdafunctionlist)
                    {
                        string parentfunctionno = pdafunctionitem.ParentFunctionNo;
                        if (string.IsNullOrEmpty(parentfunctionno))
                        {
                            parentfunctionno = "F";
                        }
                        //pdafunction += pdafunctionitem.PDAFunctionNo + "&" + pdafunctionitem.PDAFunctionName + "&" + parentfunctionno + ";";
                        pdafunction += pdafunctionitem.PDAFunctionNo+ ";";
                    }
                }
                else
                {

                    //查找对应的PDA功能
                    List<V_PDAUsersFunctions> functionlist = puserFunRep.GetList<V_PDAUsersFunctions>().Where(m => m.UsersID == usersid).OrderBy(m => m.PDAFunctionNo).ToList();
                    if (functionlist != null && functionlist.Count > 0)
                    {
                        foreach (var functionitem in functionlist)
                        {
                            string parentfunctionno = functionitem.ParentPDAFunctionNo;
                            if (string.IsNullOrEmpty(parentfunctionno))
                            {
                                parentfunctionno = "F";
                            }
                           // pdafunction += functionitem.PDAFunctionNo + "&" + functionitem.PDAFunctionName + "&" + parentfunctionno + ";";
                            pdafunction += functionitem.PDAFunctionNo + ";";
                        }
                    }
                }
            }
            else
            {
                returnvalue = "-1";
                //插入PDA访问日志
                PDAConnectLog pclinfo = new PDAConnectLog();
                pclinfo.ID = Guid.NewGuid().ToString();
                pclinfo.LogContent = pdano;
                pclinfo.ConnectFlag = "非法";
                pclinfo.PortName = "获取PDA功能和快递类别";
                pclinfo.CreateDate = System.DateTime.Now;
                plRep.Add(pclinfo);
                plRep.SaveChanges();
            }
            byte[] Arrayexpressclass = Encoding.UTF8.GetBytes(expressclass);
            string xmlexpressclass = Encoding.UTF8.GetString(Arrayexpressclass);

            byte[] Arraypdafunction = Encoding.UTF8.GetBytes(pdafunction);
            string xmlpdafunction = Encoding.UTF8.GetString(Arraypdafunction);

            elementReturnValue.InnerText = returnvalue;
            elementExpressClass.InnerText = xmlexpressclass;
            elementPDAFunction.InnerText = xmlpdafunction;

            elementWebService.AppendChild(elementReturnValue);
            elementWebService.AppendChild(elementExpressClass);
            elementWebService.AppendChild(elementPDAFunction);

            return xd;

        }


        /// <summary>
        /// 获取服务器时间
        /// </summary>
        /// <returns></returns>
        [WebMethod]
        public XmlDocument GetServerTime()
        {
            //准备返回结果
            XmlDocument xd = new XmlDocument();
            var el = xd.CreateElement("JCTimes");
            xd.AppendChild(el);

            // WebService 
            XmlElement elementWebService = xd.CreateElement("WebService");
            el.AppendChild(elementWebService);

            // ReturnValue
            XmlElement elementReturnValue = xd.CreateElement("ReturnValue");

            elementReturnValue.InnerText = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            elementWebService.AppendChild(elementReturnValue);

            return xd;

        }
    }
}
